require 'rexml/document'
require 'xml/xslt'

class PagesController < ApplicationController

  @@xslt = XML::XSLT.new
  @@xslt.xsl = "#{RAILS_ROOT}/script/js-code.xsl"
  
  before_filter :validate_session
  before_filter :init_site
  before_filter :update_site #, :only => ['show', 'update']
  
  # GET /pages
  # GET /pages.xml
  def index
    @pages = Page.find(:all)

    respond_to do |format|
      format.html # index.erb
      format.xml  { render :xml => @pages.to_xml }
    end
#  rescue ActiveResource::ConnectionError
#    redirect_to session_path
  end

  # GET /pages/1
  # GET /pages/1.xml
  def show
    @page = Page.find(params[:id])

    respond_to do |format|
      format.html # show.erb
      format.xml  { render :xml => @page.to_xml }
    end
  end

  # GET /pages/new
  def new
    @page = Page.new
  end

  # GET /pages/1;edit
  def edit
    @page = Page.find(params[:id])
  end

  # POST /pages
  # POST /pages.xml
  def create
    @page = Page.new(params[:page])

    respond_to do |format|
      if @page.save
        flash[:notice] = 'Page was successfully created.'
        format.html { redirect_to page_url(@page) }
        format.xml  { head :created, :location => page_url(@page) }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @page.errors.to_xml }
      end
    end
  end

  # PUT /pages/1
  # PUT /pages/1.xml
  def update
    @page = Page.find(params[:id])

    respond_to do |format|
#      if @page.update_attributes(params[:page])
      @page.title = params[:page][:title]
      @page.source = params[:code] ? CGI.escapeHTML(CGI.escapeHTML(params[:page][:source])) : params[:page][:source]
      @page.save
        flash[:notice] = 'Page was successfully updated.'
        format.html { redirect_to page_url(@page) }
        format.xml  { head :ok }
#      else
#        format.html { render :action => "edit" }
#        format.xml  { render :xml => @page.errors.to_xml }
#      end
    end
  end

  # DELETE /pages/1
  # DELETE /pages/1.xml
  def destroy
    @page = Page.find(params[:id])
    @page.destroy

    respond_to do |format|
      format.html { redirect_to pages_url }
      format.xml  { head :ok }
    end
  end
  
  def run
    @page = Page.find(params[:id])
    session[:runner] = @page.creator
    @@xslt.xml = REXML::Document.new "<!DOCTYPE html [ \
     <!ENTITY nbsp   \"&#160;\"> \
     ]> \
     <body>#{@page.source}</body>"
    @page.source = @@xslt.serve
  end

  def preview
    @page = Page.find(params[:id])
  end

  private
  def init_site
    Page.site = "https://#{CGI.escape session[:id]}:#{session[:key]}.#{CONFIG['app_key']}@api.springnote.com" 
  end

  def update_site
    runners = CONFIG['runners']
    runners.each do |runner|
      if runner['runner']['id'] == session[:runner]
        Page.site = "https://#{CGI.escape session[:runner]}:#{runner['runner']['key']}.#{CONFIG['app_key']}@api.springnote.com" 
      end
    end
  end

  def validate_session
    unless session[:id] && session[:key]
      session[:location] = request.request_uri
      redirect_to :controller => 'auth', :action => 'login'
      return false
    end
  end
end
